//
//  UIViewController+PageParser.m
//  Pods
//
//  Created by 网新中研 on 17/4/5.
//
//

#import "UIViewController+PageParser.h"
#import <CSUIKit/CSUIKit.h>

@implementation UIViewController (PageParser)

-(UIView *)getCSPageView:(NSArray *)descriptionDatas isNavigationBarVisible:(BOOL)isNavigationBarVisible{
    
    NSArray *csViews = [self getCSViews:descriptionDatas];
    UIView *pageView = [self getPageView:csViews isNavigationBarVisible:isNavigationBarVisible];
    return pageView;
    
}

-(UIView *)getPageView:(NSArray *)viewArray isNavigationBarVisible:(BOOL)isNavigationBarVisible;{
    
    UIView *pageView = [[UIView alloc]init];
    
    CGFloat primaryY = 0;
    
    if (isNavigationBarVisible) {
        pageView.frame = CGRectMake(0, HEIGHT_NAVIGATIONBAR, kWindowW, HEIGHT_CONTENT);
    }else{
        pageView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
    }
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.frame = pageView.frame;
    [pageView addSubview:scrollView];
    
    for (CSBaseView *csView in viewArray ) {
        
        primaryY = [csView layoutView:scrollView primaryY:primaryY];
        
    }
    
    scrollView.contentSize = CGSizeMake(0, primaryY);
    
    return pageView;
}

-(NSArray *)getCSViews:(NSArray *)descriptionDatas{
    
    NSMutableArray *resultArray = [[NSMutableArray alloc]init];
    
    if (descriptionDatas != nil) {
        
        for (NSArray *description in descriptionDatas) {
            
            if (description.count <= 0) {
                continue;
            }
            
            int type = [[description objectAtIndex:0] intValue];
            
            if (type == CSViewType_PageSectionTitleView) {
                
                //标题栏视图
                NSString *titleString = [description objectAtIndex:1];
                NSString *titleColor = [description objectAtIndex:2];
                NSString *imageUrl = nil;
                if (description.count > 3) {
                    imageUrl = [description objectAtIndex:3];
                }
                
                CS_Page_SectionTitleView *pageSectionTitleView =
                    [CS_Page_SectionTitleView page_SectionTitleView:titleString color:[UIColor colorFromHexCode:titleColor] image:nil];
                [resultArray addObject:pageSectionTitleView];
                
            }else if (type == CSViewType_PageTextView){
                
                //文档视图
                NSString *titleString = [description objectAtIndex:1];
                NSString *jsonFieldString = [description objectAtIndex:2];
                NSString *dataValue = nil;
                
                if (description.count > 3) {
                    dataValue = [description objectAtIndex:3];
                }
                
                CS_Page_TextView *pageTextView = [CS_Page_TextView page_textView:titleString dataValue:dataValue];
                pageTextView.jsonField = jsonFieldString;
                
                [resultArray addObject:pageTextView];
                
            }else if (type == CSViewType_PageImageView){
                
                //图片视图
                NSString *titleString = [description objectAtIndex:1];
                NSString *jsonFieldString = [description objectAtIndex:2];
                NSArray *dataValue = nil;
                
                if (description.count > 3) {
                    dataValue = [description objectAtIndex:3];
                }
                
                CS_Page_ImageView *pageTextView = [CS_Page_ImageView page_ImageView:titleString images:dataValue mark:nil];
                pageTextView.jsonField = jsonFieldString;
                
                [resultArray addObject:pageTextView];
                
            }
        }
    }
    
    return resultArray;
}
@end
